﻿using DevExpress.XtraEditors;
using RecruitStudents.Client.BLL;
using RecruitStudents.Utility;
using RecruitStudents.Utility.Helper;
using RecruitStudents.Utility.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RecruitStudents.Client.Base
{
    public partial class Base_Pad_ExamScorePageSetting_Option : BaseForm
    {
        public class ComboxData
        {
            public int Index { set; get; }
            public string Text { set; get; }
            public string Value { set; get; }
            public override string ToString()
            {
                return Text;
            }
        }
        DataTable dtDicPadFiledALL = null;
        ServiceBase service = new ServiceBase();
        string PadExamScorePageSettingType = string.Empty;
        TB_Dic_PadExamScorePageSetting DicPadSettingModel = null;
        List<TB_Dic_PadExamScorePageSetting_Detail> DicPadSettingList = null;
        public Base_Pad_ExamScorePageSetting_Option(string strPadExamScorePageSettingType)
        {
            InitializeComponent();     
            PadExamScorePageSettingType = strPadExamScorePageSettingType;
            dtDicPadFiledALL = service.GetDicPadShowFiledAllTable("1");
            if (strPadExamScorePageSettingType.ToSafeString().Length > 0)
            {
                DicPadSettingModel = service.GetPadExamScorePageSettingModel(strPadExamScorePageSettingType);
                if (DicPadSettingModel != null)
                {
                    DicPadSettingList = service.GetPadExamScorePageSettingDetailList(DicPadSettingModel.PadExamScorePageSettingID);
                    DicPadSettingModel.Attach();
                    txtColumnCount.Text = DicPadSettingModel.ColumnCount.ToSafeString();
                    txtRowCount.Text = DicPadSettingModel.RowCount.ToSafeString();
                    CreateTable(DicPadSettingModel.ColumnCount.ToSafeInt(), DicPadSettingModel.RowCount.ToSafeInt());

                }
            }
        }

        private void CreateTable(int ColumnCount, int RowCount)
        {
            xtraScrollableControl1.Controls.Clear();
            TableLayoutPanel tableLayoutPanel1 = new TableLayoutPanel();
            tableLayoutPanel1.Name = "tableLayoutPanel1";
            tableLayoutPanel1.Controls.Clear();
            tableLayoutPanel1.CellBorderStyle = System.Windows.Forms.TableLayoutPanelCellBorderStyle.Single;
            tableLayoutPanel1.ColumnCount = ColumnCount;
            tableLayoutPanel1.RowCount = RowCount;
            for (int i = 0; i < ColumnCount; i++)
            {
                tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 150));
            }
            for (int i = 0; i < RowCount; i++)
            {
                tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 28));
            }
            tableLayoutPanel1.Size = new System.Drawing.Size(100 * ColumnCount, 30 * RowCount);
            for (int i = 0; i < RowCount; i++)
            {
                TB_Dic_PadExamScorePageSetting_Detail modelDetail = null;
                if (DicPadSettingList != null)
                {
                    modelDetail = DicPadSettingList[i];
                }
                for (int c = 0; c < ColumnCount; c++)
                {
                    Panel panl = new Panel();
                    panl.Name = "panl" + i.ToIntString() + "_" + c.ToIntString();
                    ComboBoxEdit comboBox = new ComboBoxEdit();
                    comboBox.Width = 110;
                    comboBox.Location = new System.Drawing.Point(1, 1);
                    comboBox.Name = "comboBox" + i.ToIntString() + "_" + c.ToIntString();
                    for (int d = 0; d < dtDicPadFiledALL.Rows.Count; d++)
                    {
                        comboBox.Properties.Items.Add(new ComboxData() { Index = d, Text = dtDicPadFiledALL.Rows[d]["DicName"].ToString(), Value = dtDicPadFiledALL.Rows[d]["DicNO"].ToString() });
                    }
                    panl.Controls.Add(comboBox);
                    CheckEdit chkBox = new CheckEdit();
                    chkBox.Name = "chkBox" + i.ToIntString() + "_" + c.ToIntString();
                    chkBox.Location = new System.Drawing.Point(120, 3);
                    chkBox.Text = "";
                    panl.Controls.Add(chkBox);
                    if (modelDetail != null)
                    {
                        if (c == 0)
                        {
                            try
                            {
                                if (modelDetail.Column1.ToSafeString().Length > 0)
                                {
                                    int len = -1;
                                    foreach (ComboxData item in comboBox.Properties.Items)
                                    {
                                        if (item.Text == modelDetail.Column1 || item.Value == modelDetail.Column1)
                                        {
                                            len = item.Index;
                                            break;
                                        }
                                    }
                                    if (len > -1)
                                    {
                                        comboBox.SelectedIndex = len;
                                    }
                                    else
                                    {
                                        comboBox.EditValue = modelDetail.Column1;
                                    }
                                }
                            }
                            catch
                            {
                                comboBox.EditValue = modelDetail.Column1;
                            }
                            chkBox.Checked = modelDetail.Column1IsTitle == "1" ? true : false;
                        }
                        else if (c == 1)
                        {
                            try
                            {
                                if (modelDetail.Column2.ToSafeString().Length > 0)
                                {
                                    int len = -1;
                                    foreach (ComboxData item in comboBox.Properties.Items)
                                    {
                                        if (item.Text == modelDetail.Column2 || item.Value == modelDetail.Column2)
                                        {
                                            len = item.Index;
                                            break;
                                        }
                                    }
                                    if (len > -1)
                                    {
                                        comboBox.SelectedIndex = len;
                                    }
                                    else
                                    {
                                        comboBox.EditValue = modelDetail.Column2;
                                    }
                                }
                            }
                            catch
                            {
                                comboBox.EditValue = modelDetail.Column2;
                            }
                            chkBox.Checked = modelDetail.Column2IsTitle == "1" ? true : false;
                        }
                        else if (c == 2)
                        {
                            try
                            {
                                if (modelDetail.Column3.ToSafeString().Length > 0)
                                {
                                    int len = -1;
                                    foreach (ComboxData item in comboBox.Properties.Items)
                                    {
                                        if (item.Text == modelDetail.Column3 || item.Value == modelDetail.Column3)
                                        {
                                            len = item.Index;
                                            break;
                                        }
                                    }
                                    if (len > -1)
                                    {
                                        comboBox.SelectedIndex = len;
                                    }
                                    else
                                    {
                                        comboBox.EditValue = modelDetail.Column3;
                                    }
                                }
                            }
                            catch
                            {
                                comboBox.EditValue = modelDetail.Column3;
                            }
                            chkBox.Checked = modelDetail.Column3IsTitle == "1" ? true : false;
                        }
                        else if (c == 3)
                        {
                            try
                            {
                                if (modelDetail.Column4.ToSafeString().Length > 0)
                                {
                                    int len = -1;
                                    foreach (ComboxData item in comboBox.Properties.Items)
                                    {
                                        if (item.Text == modelDetail.Column4 || item.Value == modelDetail.Column4)
                                        {
                                            len = item.Index;
                                            break;
                                        }
                                    }
                                    if (len > -1)
                                    {
                                        comboBox.SelectedIndex = len;
                                    }
                                    else
                                    {
                                        comboBox.EditValue = modelDetail.Column4;
                                    }
                                }
                            }
                            catch
                            {
                                comboBox.EditValue = modelDetail.Column4;
                            }
                            chkBox.Checked = modelDetail.Column4IsTitle == "1" ? true : false;
                        }
                        else if (c == 4)
                        {
                            try
                            {
                                if (modelDetail.Column5.ToSafeString().Length > 0)
                                {
                                    int len = -1;
                                    foreach (ComboxData item in comboBox.Properties.Items)
                                    {
                                        if (item.Text == modelDetail.Column5 || item.Value == modelDetail.Column5)
                                        {
                                            len = item.Index;
                                            break;
                                        }
                                    }
                                    if (len > -1)
                                    {
                                        comboBox.SelectedIndex = len;
                                    }
                                    else
                                    {
                                        comboBox.EditValue = modelDetail.Column5;
                                    }
                                }
                            }
                            catch
                            {
                                comboBox.EditValue = modelDetail.Column5;
                            }
                            chkBox.Checked = modelDetail.Column5IsTitle == "1" ? true : false;
                        }
                        else if (c == 5)
                        {
                            try
                            {
                                if (modelDetail.Column6.ToSafeString().Length > 0)
                                {
                                    int len = -1;
                                    foreach (ComboxData item in comboBox.Properties.Items)
                                    {
                                        if (item.Text == modelDetail.Column6 || item.Value == modelDetail.Column6)
                                        {
                                            len = item.Index;
                                            break;
                                        }
                                    }
                                    if (len > -1)
                                    {
                                        comboBox.SelectedIndex = len;
                                    }
                                    else
                                    {
                                        comboBox.EditValue = modelDetail.Column6;
                                    }
                                }
                            }
                            catch
                            {
                                comboBox.EditValue = modelDetail.Column6;
                            }
                            chkBox.Checked = modelDetail.Column6IsTitle == "1" ? true : false;
                        }
                    }
                    tableLayoutPanel1.Controls.Add(panl, c, i);
                }
            }
            tableLayoutPanel1.Dock = DockStyle.Top;
            xtraScrollableControl1.Controls.Add(tableLayoutPanel1);
        }

        private List<TB_Dic_PadExamScorePageSetting_Detail> GetControlValue(string strGID)
        {
            List<TB_Dic_PadExamScorePageSetting_Detail> list = new List<TB_Dic_PadExamScorePageSetting_Detail>();
            Control.ControlCollection controls = xtraScrollableControl1.Controls;
            if (controls == null || controls.Count == 0)
            {
                DevHelper.ShowErrorMessageBox("您还没有生成操作行列!");
                return null;
            }
            else
            {
                TableLayoutPanel tableLayoutPanel1 = (TableLayoutPanel)controls[0];
                int ColumnCount = tableLayoutPanel1.ColumnCount;
                int RowCount = tableLayoutPanel1.RowCount;
                for (int i = 0; i < RowCount; i++)
                {
                    TB_Dic_PadExamScorePageSetting_Detail modelDetail = GetModelDetail(strGID, tableLayoutPanel1, ColumnCount, i);
                    list.Add(modelDetail);
                }
            }
            return list;
        }

        private static TB_Dic_PadExamScorePageSetting_Detail GetModelDetail(string strGID, TableLayoutPanel tableLayoutPanel1,
            int ColumnCount, int i)
        {
            TB_Dic_PadExamScorePageSetting_Detail modelDetail = new TB_Dic_PadExamScorePageSetting_Detail();
            modelDetail.PadExamScorePageSettingID = strGID;
            modelDetail.Sort = i;
            for (int c = 0; c < ColumnCount; c++)
            {
                Panel panl = (Panel)tableLayoutPanel1.Controls["panl" + i.ToIntString() + "_" + c.ToIntString()];
                if (panl != null && panl.Controls != null && panl.Controls.Count > 0)
                {
                    string strColumnName = string.Empty;
                    string strColumnNameFiled = string.Empty;
                    ComboBoxEdit comboBox = (ComboBoxEdit)panl.Controls[0];
                    if (comboBox != null)
                    {
                        if (comboBox.SelectedIndex >= 0)
                        {
                            strColumnName = (comboBox.SelectedItem as ComboxData).Text;
                            strColumnNameFiled = (comboBox.SelectedItem as ComboxData).Value;
                        }
                        else
                        {
                            strColumnName = comboBox.EditValue.ToSafeString();
                            strColumnNameFiled = comboBox.EditValue.ToSafeString();
                        }
                    }
                    CheckEdit chkBox = (CheckEdit)panl.Controls[1];
                    if (chkBox.Checked)
                    {
                        #region CheckBox true
                        if (c == 0)
                        {
                            modelDetail.Column1 = strColumnName;
                            modelDetail.Column1IsTitle = "1";
                        }
                        else if (c == 1)
                        {
                            modelDetail.Column2 = strColumnName;
                            modelDetail.Column2IsTitle = "1";
                        }
                        else if (c == 2)
                        {
                            modelDetail.Column3 = strColumnName;
                            modelDetail.Column3IsTitle = "1";
                        }
                        else if (c == 3)
                        {
                            modelDetail.Column4 = strColumnName;
                            modelDetail.Column4IsTitle = "1";
                        }
                        else if (c == 4)
                        {
                            modelDetail.Column5 = strColumnName;
                            modelDetail.Column5IsTitle = "1";
                        }
                        else if (c == 5)
                        {
                            modelDetail.Column6 = strColumnName;
                            modelDetail.Column6IsTitle = "1";
                        }
                        #endregion
                    }
                    else
                    {
                        #region CheckBox false
                        if (c == 0)
                        {
                            modelDetail.Column1 = strColumnNameFiled;
                            modelDetail.Column1IsTitle = "0";
                        }
                        else if (c == 1)
                        {
                            modelDetail.Column2 = strColumnNameFiled;
                            modelDetail.Column2IsTitle = "0";
                        }
                        else if (c == 2)
                        {
                            modelDetail.Column3 = strColumnNameFiled;
                            modelDetail.Column3IsTitle = "0";
                        }
                        else if (c == 3)
                        {
                            modelDetail.Column4 = strColumnNameFiled;
                            modelDetail.Column4IsTitle = "0";
                        }
                        else if (c == 4)
                        {
                            modelDetail.Column5 = strColumnNameFiled;
                            modelDetail.Column5IsTitle = "0";
                        }
                        else if (c == 5)
                        {
                            modelDetail.Column6 = strColumnNameFiled;
                            modelDetail.Column6IsTitle = "0";
                        }
                        #endregion
                    }
                }
            }
            return modelDetail;
        }
       

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (txtColumnCount.Text.ToSafeInt() == 0 || txtRowCount.Text.ToSafeInt() == 0)
            {
                DevHelper.ShowErrorMessageBox("请输入行数和列数!");
                return;
            }
            if (txtColumnCount.Text.ToSafeInt() > 6)
            {
                DevHelper.ShowErrorMessageBox("您输入列数不能大于6!");
                return;
            }
            if (DicPadSettingModel == null)
            {
                DicPadSettingModel = new TB_Dic_PadExamScorePageSetting();
            }
            DicPadSettingModel.ColumnCount = txtColumnCount.Text.ToSafeInt();
            DicPadSettingModel.RowCount = txtRowCount.Text.ToSafeInt();
            DicPadSettingModel.PadExamScorePageSettingType = PadExamScorePageSettingType;

            //List<TB_Dic_PadExamScorePageSetting_Detail> list = GetControlValue(DicPadSettingModel.PadExamScorePageSettingID);
            //if (list != null)
            //{
                service.SavePadExamScorePageSetting(DicPadSettingModel);
            //}
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnCreateTable_Click(object sender, EventArgs e)
        {
            if (txtColumnCount.Text.ToSafeInt() == 0 || txtRowCount.Text.ToSafeInt() == 0)
            {
                DevHelper.ShowInfoMessageBox("请输入行数和列数!");
                return;
            }
            if (txtColumnCount.Text.ToSafeInt() > 6)
            {
                DevHelper.ShowErrorMessageBox("您输入列数不能大于6!");
                return;
            }
            CreateTable(txtColumnCount.Text.ToSafeInt(), txtRowCount.Text.ToSafeInt());
        }
    }
}
